﻿/****************************** Module Header ******************************\
 * Module Name:  ClickEnvntHelper.cs
 * Module Version:  1.0.0 (2013-7-25)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to extension winformcontrol instance's method.  It supplies 
 * public static extension methods to load data or get othor control.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UCL.Forms
{
    /// <summary>
    /// 单双击事件助手类,用于解决双击事件触发单击事件的问题
    /// <para>2013-7-25 by sutroon</para>
    /// <para>例如：</para>
    /// <para>ClickEnvntHelper ceh;</para>
    /// <para>private void frmMain_Load(object sender, EventArgs e)</para>
    /// <para>{</para>
    /// <para>    ceh= new ClickEnvntHelper();</para>
    /// <para>    ceh.ClickEventHandler = lvClick;</para>
    /// <para>    ceh.DoubleClickEventHandler = lvDoubleClick;</para>
    /// <para>}</para>
    /// <para>private void lv_MouseDown(object sender, MouseEventArgs e)</para>
    /// <para>{</para>
    /// <para>    if (e.Button == MouseButtons.Right) return;</para>
    /// <para>    ceh.CheckClick();</para>
    /// <para>}</para>
    /// </summary>
    public class ClickEventHelper
    {
        private Timer doubleClickTimer;
        private bool isFirstClick = true;
        private bool isDoubleClick = false;
        private int milliseconds = 0;
        public Action ClickEventHandler;
        public Action DoubleClickEventHandler;
        public ClickEventHelper()
        {
            doubleClickTimer = new Timer() { Interval = 100 };
            doubleClickTimer.Tick += new EventHandler(doubleClickTimer_Tick);
        }
        public void CheckClick()
        {
            if (isFirstClick)
            {
                isFirstClick = false;
                // Start the double click timer.
                doubleClickTimer.Start();
            }
            else
            {
                if (milliseconds < SystemInformation.DoubleClickTime)
                {
                    isDoubleClick = true;
                }
            }
        }
        void doubleClickTimer_Tick(object sender, EventArgs e)
        {
            milliseconds += 100;

            // The timer has reached the double click time limit.
            if (milliseconds >= SystemInformation.DoubleClickTime)
            {
                doubleClickTimer.Stop();

                if (isDoubleClick)
                {
                    // 双击事件
                    if (DoubleClickEventHandler!=null) DoubleClickEventHandler();
                }
                else
                {
                    // 单击事件
                    if (ClickEventHandler != null) ClickEventHandler();
                }

                // Allow the MouseDown event handler to process clicks again.
                isFirstClick = true;
                isDoubleClick = false;
                milliseconds = 0;
            }
        }
    }
}
